package com.bing.controller.LoanProducts;

import com.bing.entity.LoanProducts.LoanProductinfo;
import com.bing.service.LoanProdductsinfoService;
import com.bing.utils.idgenertor.IdGen;
import com.bing.web.BaseController;
import com.bing.web.BaseResponse;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by hurl on 2019-05-18
 * 产品
 */
@RestController
@RequestMapping("/loanProduct")
@Component
@Aspect
@CrossOrigin(maxAge = 3600)
public class LoanProdductsController  extends BaseController{

    private static final String ID = "100001";
    @Autowired
    LoanProdductsinfoService loanProdductsinfoService;

    @ApiOperation(value = "产品列表")
    @RequestMapping(value = "get")
    public ResponseEntity<BaseResponse> getProductList(@RequestParam(defaultValue = "1") Integer pageNum) {
        return buildSuccessResponse(loanProdductsinfoService.getProuctList(pageNum));
    }

    @ApiOperation(value = "产品列表")
    @RequestMapping(value = "getIdNameMap")
    public ResponseEntity<BaseResponse> getIdNameMap() {
        return buildSuccessResponse(loanProdductsinfoService.getIdNameMap());
    }

    @ApiOperation(value = "新增产品")
    @RequestMapping(value = "initorchange")
    @CrossOrigin()
    public ResponseEntity<BaseResponse> addProduct(@RequestBody LoanProductinfo loanProdductsinfo) {
        if (StringUtils.isEmpty(loanProdductsinfo.getProductId())) {
            //产品id自增
            String id = loanProdductsinfoService.getProdId();
            if (StringUtils.isEmpty(id)){
                loanProdductsinfo.setProductId(ID);
            } else {
                loanProdductsinfo.setProductId(String.valueOf(Integer.valueOf(id)+1));
            }
            loanProdductsinfoService.insertSelective(loanProdductsinfo);
        } else {
            loanProdductsinfoService.updateByPrimaryKeySelective(loanProdductsinfo);
        }
        return buildSuccessResponse();
    }

    @ApiOperation(value = "修改产品")
    @RequestMapping(value = "update")
    @CrossOrigin()
    public ResponseEntity<BaseResponse> updadteProduct(@RequestBody LoanProductinfo loanProdductsinfo) {
        loanProdductsinfoService.updateByPrimaryKeySelective(loanProdductsinfo);
        return buildSuccessResponse();
    }

    @ApiOperation(value = "查看产品明细")
    @RequestMapping(value = "detail")
    public ResponseEntity<BaseResponse> getProductDetail(@RequestParam String productId) {
        return buildSuccessResponse(loanProdductsinfoService.getProductDetail(productId));
    }


    @ApiOperation(value = "上架/下架")
    @RequestMapping(value = "updateByShelf")
    public ResponseEntity<BaseResponse> updateByShelf(@RequestBody LoanProductinfo loanProdductsinfo) {
        LoanProductinfo prodductsinfo = new LoanProductinfo();
        prodductsinfo.setProductId(loanProdductsinfo.getProductId());
        prodductsinfo.setShelf(loanProdductsinfo.getShelf());
        loanProdductsinfoService.updateByPrimaryKeySelective(prodductsinfo);
        return buildSuccessResponse();
    }
}
